<template>
  <div class="login-wrap">
    <el-form :model="userInfo" :rules="rules2" ref="userInfo" class="demo-ruleForm">
      <h3>欢迎使用随意钱包后台管理系统</h3>
      <el-form-item prop="user">
        <el-input type="text" v-model="userInfo.user" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="pass">
        <el-input type="password" v-model="userInfo.pass" placeholder="密码" @keydown.native="show($event,'userInfo')"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="js-btn" type="primary" @click="submitForm('userInfo')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {setCookie,delCookie} from "@/utils/utils";
  import {post} from '@/utils/http'
  export default {
    data() {
      let validateUser = (rule, value, callback) => {
        let reg = /^[a-zA-Z0-9_-]{4,16}$/;
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else if(!reg.test(value)){
          callback(new Error('请输入符合条件的用户名'));
        } else {
          callback();
        }
      };
      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        userInfo: {
          user: '',
          pass: ''
        },
        rules2: {
          user: [
            { validator: validateUser, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(userInfo) {
        this.$refs[userInfo].validate((valid) => {
          if (valid) {
            let _this = this;
            delCookie('token');
            delCookie('X-Auth-Token')
            localStorage.removeItem('Auth')
            post('/api/manager/v1/user/login',{'username':this.userInfo.user,'password': this.userInfo.pass})
              .then(function(response) {

                let day = response.data.expireSeconds / (3600*24);
                setCookie('token',response.data.token,day);
                localStorage.setItem('Auth',response.data.token);
                localStorage.setItem('userName',_this.userInfo.user);
                _this.$router.push('/homePage');
              })
          } else {
            return false;
          }
        });
      },
      show(ev, userInfo) {
        let self = this;
        if(ev.keyCode == 13) {
          self.$refs[userInfo].validate((valid) => {
            if (valid) {
              delCookie('token');
              delCookie('X-Auth-Token')
              localStorage.removeItem('Auth')
              post('/api/manager/v1/user/login',{'username':this.userInfo.user,'password': this.userInfo.pass})
                .then(function(response) {
                  let day = response.data.expireSeconds / (3600*24);
                  setCookie('token',response.data.token,day);
                  localStorage.setItem('Auth',response.data.token);
                  localStorage.setItem('userName',self.userInfo.user);
                  self.$router.push('/homePage');
                })
            } else {
              return false;
            }
          });

        }
      }


    }
  }
</script>

<style lang="scss" scoped>
  .login-wrap {
    height: 100%;
    background: url("../../../../static/img/bg.jpg") no-repeat 100% 100%;
    .demo-ruleForm {
      h3 {
        color: #fff;
        letter-spacing: 2px;
        text-align: center;
        font-weight: normal;
      }
      width: 450px;
      box-sizing: border-box;
      border-radius: 6px;
      padding: 20px 45px;
      border: 1px solid rgba(127,127,127,.8);
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);

    }
    .js-btn {
      margin-top: 10px;
      display: block;
      width: 100%;
    }
  }
</style>
